package com.example.dairenkun.neufoodshop.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.adapter.FoodListAdapter;
import com.example.dairenkun.neufoodshop.bean.Food;
import com.example.dairenkun.neufoodshop.bean.doCollect;
import com.example.dairenkun.neufoodshop.bean.isCollected;
import com.example.dairenkun.neufoodshop.model.FoodModel;
import com.example.dairenkun.neufoodshop.service.FoodService;


import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodActivity extends AppCompatActivity {

    private Context context;
    private RecyclerView recyclerView;
    int shopid;
    private TextView shopname;
    private TextView shopphone;
    private TextView shopphonenum;
    private TextView shopintro;
    private TextView shopintrocontent;
    private RatingBar shopstar;
    private ImageButton image_collect;
   int userid;
    String result;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_food);
        initViews();
        Intent intent =getIntent();
        shopid=Integer.valueOf(intent.getStringExtra("shop_id"));
        shopintrocontent.setText(intent.getStringExtra("shopintro"));
        shopstar.setRating(Integer.valueOf(intent.getStringExtra("shopstar")));
        shopname.setText(intent.getStringExtra("shopname"));
        shopphonenum.setText(intent.getStringExtra("shopphone"));
        getData();
        getUser();
        isCollect();

        image_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doCollect();
            }
        });

    }
    private  void initViews(){
        recyclerView=(RecyclerView)findViewById(R.id.recycler_food_id);
        recyclerView.setLayoutManager(new LinearLayoutManager(FoodActivity.this));
        shopname=(TextView)findViewById(R.id.food_shop_name);
        shopphone=(TextView)findViewById(R.id.food_shop_phone);
        shopphonenum=(TextView)findViewById(R.id.food_shop_phonenum);
        shopintro=(TextView)findViewById(R.id.food_shop_intro);
        shopintrocontent=(TextView)findViewById(R.id.food_shop_intro_content);
        shopstar=(RatingBar) findViewById(R.id.food_shop_ratingBar);
        image_collect=(ImageButton)findViewById(R.id.food_shop_collect);
    }
    private void getData() {
        Intent intent =getIntent();
        shopid=Integer.valueOf(intent.getStringExtra("shop_id"));
        //获取网上的数据/
       /* Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://60.205.189.39/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        FoodService testService=retrofit.create(FoodService.class);
        Call<List<Food>> call=testService.getShopFood(shopid);*/
        FoodModel foodModel=new FoodModel();
        Call <List<Food>>call=foodModel.getFoodList(shopid);
        call.enqueue(new Callback<List<Food>>() {
            @Override
            public void onResponse(Call<List<Food>> call, retrofit2.Response<List<Food>> response)
            {
                List<Food> food=response.body();
                recyclerView.setAdapter(new FoodListAdapter(FoodActivity.this,food));
            }
            @Override
            public void onFailure(Call<List<Food>> call, Throwable t) {

            }
        });
    }
    private  void isCollect(){

        FoodModel foodModel=new FoodModel();
        Call call=foodModel.isCollect(userid,shopid,0);
        call.enqueue(new Callback<isCollected>() {
            @Override
            public void onResponse(Call<isCollected> call, retrofit2.Response<isCollected> response)
            {
                isCollected res=response.body();
               result =res.getCollected().toString();
                if (result.equals("0"))
                {
                   image_collect.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                }
                else {
                    image_collect.setImageResource(R.drawable.ic_favorite_black_24dp);
                }

            }
            @Override
            public void onFailure(Call<isCollected> call, Throwable t) {

            }
        });
    }
    private void getUser(){

        SharedPreferences sp = getApplication().getSharedPreferences("UserInfo", getApplication().MODE_PRIVATE);
         userid = Integer.valueOf(sp.getString("user_id", null));
        //Toast.makeText(context,userid,Toast.LENGTH_SHORT).show();

    }


    private  void  doCollect(){
        FoodModel foodModel=new FoodModel();
        Call call=foodModel.doCollect(userid,shopid);
        call.enqueue(new Callback<doCollect>() {
            @Override
            public void onResponse(Call<doCollect> call, retrofit2.Response<doCollect> response)
            {
                doCollect tmp=response.body();
                String   status =tmp.getSuccess();
                if (status.equals("1"))
                {
                    if (result.equals("0"))
                    {
                        image_collect.setImageResource(R.drawable.ic_favorite_black_24dp);
                        Toast.makeText(FoodActivity.this, "收藏成功！", Toast.LENGTH_SHORT).show();
                        result="1";
                    }else
                    {
                        image_collect.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                        Toast.makeText(FoodActivity.this, "取消收藏成功！", Toast.LENGTH_SHORT).show();
                        result="0";
                    }

                }
                else {
                    Toast.makeText(FoodActivity.this, "取消失败！", Toast.LENGTH_SHORT).show();
                }

            }
            @Override
            public void onFailure(Call<doCollect> call, Throwable t) {

            }
        });
    }

}
